package com.iims.supplier.service;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.constant.UserConstants;
import com.iims.supplier.domain.Supplier;
import com.iims.supplier.mapper.SupplierMapper;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/6/11 14:27
 */
@Service
public class SupplierServiceImpl extends ServiceImpl<SupplierMapper, Supplier> implements ISupplierService {

    @Override
    public List<Supplier> selectSupplierList(Supplier supplier) {
        return baseMapper.selectSupplierList(supplier);
    }

    @Override
    public Supplier selectOne(Long supplierId) {
        return baseMapper.selectSupplierById(supplierId);
    }

    @Override
    public String checkSupplierNameUnique(Supplier supplier) {
        Long supplierId = Validator.isNull(supplier.getSupplierId()) ? -1L : supplier.getSupplierId();
        Supplier info = getOne(new LambdaQueryWrapper<Supplier>()
                .eq(Supplier::getSupplierName, supplier.getSupplierName()).last("limit 1"));
        if (Validator.isNotNull(info) && info.getSupplierId().longValue() != supplierId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public int insertSupplier(Supplier Supplier) {
        return baseMapper.insert(Supplier);
    }

    @Override
    public int updateSupplier(Supplier Supplier) {
        return baseMapper.updateById(Supplier);
    }

    @Override
    public int deleteSupplierById(Long supplierId) {
        return baseMapper.deleteById(supplierId);
    }

    @Override
    public boolean checkSupplierUse(Long supplierId) {
        return baseMapper.checkSupplierUseCount(supplierId) > 0;
    }

}
